#include "devicedialog.h"
#include "ui_devicedialog.h"
#include "rpi_device.h"
#include <QDebug>

DeviceDialog::DeviceDialog(QWidget *parent,rpi_device *d,QStringList *dl) :
    QDialog(parent),
    ui(new Ui::DeviceDialog)
{
    ui->setupUi(this);
    dev = d;
    type_v =d->type();
    qDebug() << type_v;
    type_list << "Accelerometer" << "Gyroscope" << "Pressure"
              << "Global Positioning System" << "Ultrasonic"
              << "Control Surface"
                 << "Servo"
                 << "Bidirectional Motor"
                 << "Electronic Control Motor";

    ui->deviceName->setText(type_list[type_v]);
    setWindowTitle(QString("Configure ") + type_list[type_v]);
    if (dl) {
        device_list = *dl;
    }else{
        device_list << "/dev/i2c_dev"
                       << "/dev/spi_0.0"
                          << "/dev/spi1.0"
                             << "/dev/usbtty0"
                             << "/dev/mmm0";
    }


    qDebug() << "Address" << d->address();
    qDebug() << "Node" << d->node();
    qDebug() << "Device" << d->device();
    qDebug() << "X" << d->xDev();
    qDebug() << "Y" << d->yDev();
    qDebug() << "Z" << d->zDev();
    qDebug() << "Theta" << d->thetaDev();
    qDebug() << "Phi" << d->phiDev();
    qDebug() << "Sigma" << d->sigmaDev();

    ui->addressSpinBox->setValue(d->address());
    ui->nodeSpinBox->setValue(d->node());
    ui->locationComboBox->setCurrentText(d->device());
    ui->xSpinBox->setValue(d->xDev());
    ui->ySpinBox->setValue(d->yDev());
    ui->zSpinBox->setValue(d->zDev());
    ui->thetaSpinBox->setValue(d->thetaDev());
    ui->phiSpinBox->setValue(d->phiDev());
    ui->sigmaSpinBox->setValue(d->sigmaDev());

    connect(ui->addressSpinBox,SIGNAL(valueChanged(int)),this,SLOT(setHexAddress()));
    setHexAddress();
}


void DeviceDialog::setHexAddress() {
    char a = ui->addressSpinBox->value();
    ui->labelAddress->setText( QString(a).toLocal8Bit().toHex() );
}

void DeviceDialog::accept() {


    dev->setAddress(ui->addressSpinBox->value());
    dev->setNode(ui->nodeSpinBox->value());
    dev->setDevice(ui->locationComboBox->currentText());
    dev->setXDev(ui->xSpinBox->value());
    dev->setYDev(ui->ySpinBox->value());
    dev->setZDev(ui->zSpinBox->value());

    dev->setThetaDev(ui->thetaSpinBox->value());
    dev->setPhiDev(ui->phiSpinBox->value());
    dev->setSigmaDev(ui->sigmaSpinBox->value());

    QDialog::accept();

}

DeviceDialog::~DeviceDialog()
{
    delete ui;
}
